package com.winchain.chainbill.bill.biz.discount.rx;

import com.shell.framework.command.ChainBase;
import com.shell.framework.command.CommandContext;
import com.shell.framework.rx.OnErrorNotImplementedSubscriber;
import com.winchain.chainbill.basic.charge.cmd.ValRetBizCmd;
import com.winchain.chainbill.bill.base.po.CbDct;
import com.winchain.chainbill.bill.base.po.CbOpt;
import com.winchain.chainbill.bill.biz.Bill;
import com.winchain.chainbill.bill.biz.BillTranEvent;
import com.winchain.chainbill.bill.biz.contract.cmd.ContractCancelCmd;
import com.winchain.chainbill.bill.biz.discount.cmd.BillDctCmd;
import com.winchain.chainbill.bill.biz.send.cmd.BillInvoiceRejectCmd;
import com.winchain.chainbill.bill.biz.send.cmd.BillMoveCmd;
import com.winchain.chainbill.bill.biz.send.cmd.BillOptCmd;
import com.winchain.chainbill.bill.biz.send.cmd.BillTimeoutCmd;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class BillDctRevRejectChdSubscriber extends OnErrorNotImplementedSubscriber<BillTranEvent<CbDct>> {
	@Autowired
	private BillTimeoutCmd<CbDct> billTimeoutCmd;
	@Autowired
	private BillOptCmd<CbDct> billOptCmd;
	@Autowired
	private BillMoveCmd<CbDct> billMoveCmd;
	@Autowired
	private BillDctCmd billDctCmd;
	@Autowired
	private ContractCancelCmd<CbDct> contractCancelCmd;
	@Autowired
	private ValRetBizCmd valRetBizCmd;
	@Autowired
	private BillInvoiceRejectCmd billInvoiceRejectCmd;

	public void onApplicationEvent(BillTranEvent<CbDct> t) {
		List<Bill<CbDct>> child = (List<Bill<CbDct>>) t.getChildren();
		for (Bill<CbDct> source : child) {
			CbOpt opt = new CbOpt();
			opt.setCurState("S000");
			opt.setOptCode("E009");
			opt.setRemark("������������������");
			t.setOperation(opt);
			CommandContext<Bill<CbDct>> context = new CommandContext();
			context.setSource(source);
			context.setValue("Operation", t.getOperation());
			context.setValue("TimeoutOperation", t.getTimeoutOpt());
			context.setValue("valRetBizState", "D");

			ChainBase<Bill<CbDct>> crb = new ChainBase();
			crb.add(this.billTimeoutCmd);
			crb.add(this.billOptCmd);
			crb.add(this.billMoveCmd);
			if (((CbDct) source.getSource()).getDctId() != null) {
				((CbDct) source.getSource()).setDctState("0");
				((CbDct) source.getSource()).setPmtState("1");
				((CbDct) source.getSource()).setDctQtOpt("1");

				crb.add(this.billDctCmd);
				crb.add(this.valRetBizCmd);
				crb.add(this.contractCancelCmd);
				context.setValue("bizId", ((CbDct) source.getSource()).getDctId());
				context.setValue("invType", "4");
				crb.add(this.billInvoiceRejectCmd);
			}
			crb.execute(context);
		}
	}
}
